
from lj_action import action_data
from lj_tool import tool_redis, tool_try
from lj_tool.tool_opt import *

action_alarm_event = action_data.init_action_data("alarm_event")
action_alarm_records = action_data.init_action_data("alarm_records")
alarm_event_entity = action_alarm_event.entity


def alarm_event(**alarm):
    # @tool_try.lj_no_except
    def fun():
        # 获取告警事件
        alarm_event_by_code = alarm_event_entity.get(
            alarm_event_entity.code == alarm.get("alarm_event_code"))

        # 保存告警记录
        alarm_records_do = {}
        alarm_records_do["title"] = v_of_null(alarm, "alarm_title")
        alarm_records_do["desc"] = v(alarm, "alarm_desc")

        alarm_records_do["event_id"] = v_of_null(alarm_event_by_code, "id")
        alarm_records_do["handler_id"] = v(alarm, "handler")
        alarm_records_do["status"] = "1"
        alarm_records_do["is_read"] = "1"
        alarm_records_do["push_status"] = "1"
        alarm_records_do["handler_type"] = v(alarm, "handler_type")
        alarm_records_do["tenant_id"] = v(alarm_event_by_code, "tenant_id")
        alarm_records_do["source_code"] = v(alarm, "source_code")
        alarm_records_do["ext_desc"] = v(alarm, "ext_desc")
        alarm_records_do["point_code"] = v(alarm, "point_code")
        alarm_records_do["handler_name"] = v(alarm, "handler_name")
        alarm_records_do["point_name"] = v(alarm, "point_name")
        alarm_records_do["alarm_file"] = v(alarm, "alarm_file")

        # 消息推送
        if v(alarm, "push_alias"):
            # push_id = UtilFmMsg.msg_push_app(alarm.getPointCode(
            # ), alarm_event_by_code.getName(), "其他", alarm.getPushAlias())
            # alarm_records_do.setPushId(push_id)
            pass
        if v(alarm, "push_phone"):
            # push_id = UtilFmMsg.msg_push_sms(alarm.getPointCode(
            # ), alarm_event_by_code.getName(), "其他", alarm.getPushPhone())
            # alarm_records_do.setPushId(push_id)
            pass

        # 推送状态
        alarm_records_do["push_status"] = "2"
        # 保存
        action_alarm_records.sync(alarm_records_do)

    tool_redis.init_default().lock_run(
        f"alarm_event_{v(alarm, 'point_code')}_{v(alarm, 'alarm_event_code')}", fun, 60 * 60 * 6)
